package org.szgb.console.sys.equipment.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.sys.equipment.entity.SysEquipment;
import org.szgb.console.sys.equipment.mapper.SysEquipmentMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class SysEquipmentService {
    private final SysEquipmentMapper sysEquipmentMapper;

    @Autowired
    public SysEquipmentService(SysEquipmentMapper sysEquipmentMapper) {
        this.sysEquipmentMapper = sysEquipmentMapper;
    }

    @Transactional(readOnly = true)
    public SysEquipment get(String id) {
        return sysEquipmentMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<SysEquipment> findList(SysEquipment sysEquipment) {
        if (sysEquipment != null && sysEquipment.getPageDto() != null) {
            Page<SysEquipment> page = new Page<>(sysEquipment.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            sysEquipment.setPage(page);
        }
        return sysEquipmentMapper.findList(sysEquipment);
    }

    @Transactional(readOnly = true)
    public List<String> getNameListByIdArray(String[] ids) {
        return sysEquipmentMapper.getNameListByIdArray(ids);
    }

    @Transactional(readOnly = true)
    public Integer getCount(SysEquipment sysEquipment) {
        return sysEquipmentMapper.getCount(sysEquipment);
    }

    @Transactional()
    public void save(SysEquipment sysEquipment) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (sysEquipment.getId() == null || sysEquipment.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            sysEquipment.setCreatedBy(sysEquipment.getUpdatedBy());
            // 添加UUID
            sysEquipment.setId(UUIDUtil.getUUID());
            // 执行插入操作
            sysEquipmentMapper.insert(sysEquipment);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            sysEquipmentMapper.update(sysEquipment);
        }
    }

    @Transactional
    public void delete(String ids) {
        sysEquipmentMapper.delete(ids.split(","));
    }
}
